/*!
 * 界面入口模块  
 */

layui.extend({
    setter: 'config' //配置模块
    ,
    admin: 'lib/admin' //核心模块
    ,
    view: 'lib/view', //视图渲染模块
    common: 'modules/common'
}).define(['setter', 'admin', 'common'], function (exports) {
    var setter = layui.setter,
        element = layui.element,
        admin = layui.admin,
        tabsPage = admin.tabsPage,
        view = layui.view,
        //打开标签页
        openTabsPage = function (url, text, that) {
            //遍历页签选项卡
            var matchTo, tabs = $('#LAY_app_tabsheader>li'),
                path = url.replace(/(^http(s*):)|(\?[\s\S]*$)/g, '');
            let info = { ...that[0].dataset };
            tabs.each(function (index) {
                var li = $(this),
                    layid = li.attr('lay-id');

                if (layid === info.moduleid) {
                    matchTo = true;
                    tabsPage.index = index;
                }
            });
            text = text || '新标签页';
            //定位当前tabs
            var setThisTab = function () {
                element.tabChange(FILTER_TAB_TBAS, info.moduleid);
                admin.tabsBodyChange(tabsPage.index, {
                    url: url,
                    text: text
                });
            };
            if (setter.pageTabs) {
                //如果未在选项卡中匹配到，则追加选项卡
                if (!matchTo) {
                    $.ajax({
                        url: url,
                        async: false,
                        error: function (a) {
                            if (a.status == 404) {
                                info.url = "views/home/404.html";
                            }
                        }
                    });
                    var loading = `<div class="pear-frame-loading">
			                                <div class="ball-loader">
			                                <span></span><span></span><span></span><span></span>
			                                </div>
			                           </div>`;
                    $(APP_BODY).append([
                        '<div class="layadmin-tabsbody-item layui-show">',
                        `<iframe src="${info.url}?v=${setter.version}" name="${tabs.length}" data-moduleId="${info.moduleid}" data-moduleName="${info.modulename}"` + '" frameborder="0" class="layadmin-iframe"></iframe>',
                        '</div>'
                    ].join(''));
                    $(APP_BODY).append(loading);
                    var $frame = $(APP_BODY).find(`[data-moduleId="${info.moduleid}"]`);
                    var $frameLoad = $(APP_BODY).find(".pear-frame-loading");
                    $frameLoad.css({display: "block" });
                    $($frame).on("load", function () {
                        $frameLoad.fadeOut(800, function () {
                            $frameLoad.remove();
                        });
                    })

                    tabsPage.index = tabs.length;
                    element.tabAdd(FILTER_TAB_TBAS, {
                        title: '<span style="">' + /*that.find('i').prop('outerHTML')*/ text + '</span>',
                        id: info.moduleid,
                        attr: path
                    });
                    tabsPage.url = url;
                    tabsPage.text = text;
                    tabsPage.dataset = { ...that[0].dataset };
                    let open_tabs = openedTabs();
                    open_tabs.tabs.push({ title_text: text, id: info.moduleid, attr: path, tabsPage: tabsPage, ...that[0].dataset });
                    open_tabs.tabsPage = tabsPage;
                    layui.sessionData(setter.tableName, {
                        key: 'OpenTabs',
                        value: open_tabs
                    });
                }
            } else {
                var iframe = admin.tabsBody(admin.tabsPage.index).find('.layadmin-iframe');
                iframe[0].contentWindow.location.href = url;
                iframe[0].onload = function () {
                    const iframeNode = iframe[0].document;
                };
            }
            setThisTab();
        },
        openedTabs = function () {
            let tabs = { tabs: [], tabsPage: {} };
            return layui.sessionData(setter.tableName)["OpenTabs"] || tabs;
        }
    APP_BODY = '#LAY_app_body', FILTER_TAB_TBAS = 'layadmin-layout-tabs';

    Array.prototype.remove = function (val) {
        var index = this.indexOf(val);
        if (index > -1) {
            this.splice(index, 1);
        }
    };
    $ = layui.$, $win = $(window);

    //初始
    if (admin.screen() < 2) admin.sideFlexible();

    //将模块根路径设置为 controller 目录
    layui.config({
        base: setter.base + 'modules/',
        version: setter.version
    });

    //扩展 lib 目录下的其它模块
    layui.each(setter.extend, function (key, value) {
        var mods = {},
            _isArray = setter.extend.constructor === Array;
        mods[_isArray ? value : key] = '{/}' + setter.base + 'lib/extend/' + value;
        layui.extend(mods);
    });
    view().autoRender();
    //layui.use('common');

    //对外输出
    exports('index', {
        openTabsPage: openTabsPage
    });
});